<?php

class Friend
{
	/**
	 * 用户ID
	 * @var	int
	 */
	protected $userId;
	
	/**
	 * 单例对象
	 * @var	Friend[]
	 */
	protected static $singletonObjects = array();
	
	protected static $config = null;
	
	/**
	 * 获取实例
	 * @param	int $userId	用户ID
	 * @return	Friend
	 */
	public static function getInstance( $userId )
	{
		if( !isset( self::$singletonObjects[$userId] ) )
		{
			self::$singletonObjects[$userId] = new Friend( $userId );
		}
		
		if( !isset( self::$config ) )
		{
			self::$config = Common::getConfig();
		}
		return self::$singletonObjects[$userId];
	}
	
	/**
	 * 实例化
	 * @param	int $userId	用户ID
	 */
	protected function __construct( $userId )
	{
		$this->userId = $userId;
	}
	
	/**
	 * 获取好友ID
	 * 
	 * @param boolean $isIncludeMe 是否包括我自己
	 * @return	int[]
	 */
	public function getFriendIds( $isIncludeMe = true )
	{
		if( self::$config['etc']['friendSystem'] == 'neighbor' )
		{
			$friendIds = Neighbor::getInstance()->getNeighborIds( $this->userId );
		}
		else 
		{
			$friendIds = FM_FriendServer::getInstance( $this->userId )->getAppIds();
		}
		
		
		if( !$friendIds )
		{
			$friendIds = array();
		}
		
		if( !$isIncludeMe )
		{
			if( in_array( $this->userId , $friendIds ) )
			{
				$friendIds = array_diff( $friendIds , array( $this->userId ) );
			}
			return $friendIds;
		}
		
		if( !in_array( $this->userId , $friendIds ) )
		{
			$friendIds[] = $this->userId;
		}
		
		return $friendIds;
	}
	
	/**
	 * 获取App好友
	 * @return	int[]
	 */
	public function getAppFriendIds()
	{
		$friendIds = FM_FriendServer::getInstance( $this->userId )->getAppIds();
		
		if( !$friendIds )
		{
			$friendIds = array();
		}
		
		if( !in_array( $this->userId , $friendIds ) )
		{
			$friendIds[] = $this->userId;
		}
		
		return $friendIds;
	}


	/**
	 * 获取好友资料
	 *  @return	array
	 */
	public function getFriendInfo()
	{
		$friendInfo = FM_FriendServer::getInstance( $this->userId )->getFriendList();
		return $friendInfo ? $friendInfo : array();
	}
	
	/**
	 * 获取列表版本号（最后更新时间）
	 */
	public function getVersion()
	{
		if( self::$config['etc']['friendSystem'] == 'neighbor' )
		{
			return Neighbor::getInstance()->getLastUpdateTime( $this->userId );
		}
		else
		{
			return FM_FriendServer::getInstance( $this->userId )->getVersion();
		}
	}

	/**
	 * 获取多个用户的用户信息
	 * @param	int[] $userIds 用户ID
	 * @return	array(
	 * 				{userId}:array(
	 * 					userName:string
	 * 					headPic:string
	 * 				) ,
	 * 				...
	 * 			)
	 */
	public function getMultiUserInfo( $userIds )
	{
		$returnValue = array();
		//这个是临时实现方案
		foreach( $userIds as $userId )
		{
			$returnValue[$userId] = FM_FriendServer::getInstance( $userId )->getUserInfo();
		}
		
		return $returnValue;
	}
}